﻿Public Class SearchResults
    Inherits System.Web.UI.Page

    Public dbmanager As DBManager = New DBManager
    'This constant is to alternate between ascending and descending sort orders for the reviews listview
    'when diffSort is false, it means that the same sort by type has been clicked twice or more in a row
    Private Shared diffSort As Boolean


    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (Not Page.IsPostBack) Then
            'Set sort up/down images to be not visible
            imgDateAsc.Visible = False
            imgDateDesc.Visible = False
            If (Request.QueryString("s") IsNot Nothing) Then
                Try
                    Dim user As userAcc = Session("user")
                    dbmanager.trackSearch(Request.QueryString("s").ToString, user.userID)
                Catch ex As Exception
                    dbmanager.trackSearch(Request.QueryString("s").ToString)
                End Try

            End If
        End If

    End Sub

    Protected Sub ListView2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView2.ItemDataBound
        Dim hlOutlet As HyperLink = e.Item.FindControl("hlOutletName")
        Dim lbOutletID As Label = e.Item.FindControl("outlet_IDLabel")
        hlOutlet.NavigateUrl = "~/OutletReview.aspx?id=" + lbOutletID.Text
        Dim lbPriority As Label = e.Item.FindControl("priorityLabel")
        If lbPriority.Text = "1" Then
            Dim lbSponsored As Label = e.Item.FindControl("lblsponsored")
            lbSponsored.Visible = True
        End If
    End Sub

    Protected Sub lbSortDate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbSortDate.Click
        'Toggle between asc and desc
        If hfAscDesc.Value = "asc" Then
            hfAscDesc.Value = "desc"
            imgDateDesc.Visible = True
            imgDateAsc.Visible = False
        Else
            hfAscDesc.Value = "asc"
            imgDateDesc.Visible = False
            imgDateAsc.Visible = True
        End If

    End Sub
End Class